import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import vue from '@vitejs/plugin-vue2'
import WindiCSS from 'vite-plugin-windicss'
import SftpUploader from 'sftp-uploader'
import AutoImport from 'unplugin-auto-import/vite'

// 开发环境部署目录
const API_PREFIX = loadEnv('development', process.cwd()).VITE_APP_API_PREFIX

// 打印git日志
// const GitRevisionPlugin = require('git-revision-webpack-plugin')
// const gitRevisionPlugin = new GitRevisionPlugin({
//   versionCommand: 'describe --tags --always --dirty="-dev"',
//   commithashCommand: 'log --max-count=1 --no-merges --pretty="%ai|%s"'
// })

// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: [
      {
        find: /^~/,
        replacement: ''
      },
      {
        find: '@',
        replacement: path.resolve(__dirname, 'src')
      }
    ],
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  },
  plugins: [
    vue(),
    AutoImport({
      // targets to transform
      include: [
        /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
        /\.vue$/, /\.vue\?vue/, // .vue
        /\.md$/, // .md
      ],
      // global imports to register
      imports: [
        // presets
        'vue',
        {
          'vue': [
            ['default', 'Vue'],
          ],
          'vue-router/composables': [
            'useRoute',
            'useRouter',
            'useLink',
            'onBeforeRouteUpdate',
            'onBeforeRouteLeave',
          ],
          '@/utils/useAdmateAdapter': [
            ['default', 'useAdmateAdapter'],
          ],
        },
      ]
    }),
    WindiCSS({
      attributify: true,
      preflight: false
    }),
    // 联通云
    SftpUploader({
      dir: path.join(__dirname, 'dist/'),
      host: 'host',
      url: `/upload/${API_PREFIX}/sot-admin/`,
      port: 'port',
      username: 'username',
      password: 'password',
      previewPath: `开发环境域名/${API_PREFIX}/sot-admin/`
    })
  ],
  define: {
    'process.env': process.env,
    // 'process.VERSION': JSON.stringify(gitRevisionPlugin.version()),
    // 'process.COMMIT': JSON.stringify(gitRevisionPlugin.commithash())
  },
  base: './',
  server: {
    open: true,
    proxy: {
      // 跳转后台管理接口
      '/nanchong/sot-admin-api': {
        target: 'http://localhost:9101',
        changeOrigin: true,
        // 重写路径
        rewrite: path => path.replace(/^\/nanchong\/sot-admin-api/, '')
      },
      // 小程序资料管理接口
      '/nanchong/one-travel-api': {
        target: 'http://localhost:9080',
        changeOrigin: true,
        // 重写路径
        rewrite: path => path.replace(/^\/nanchong\/one-travel-api/, '')
      }
    }
  },
})
